/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.actor;

import org.openaecfm.model.actor.ActorRole;
import org.openaecfm.model.actor.Address;
import org.openaecfm.model.actor.Person;
import org.openaecfm.service.actor.IPersonManager;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EntityInstanceNameParameter;
import org.openaecfm.spf.model.ListParameter;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.SimpleEntityInstance;
import org.openaecfm.spf.model.StringParameter;

public final class PersonIO extends AbstractEntityInstanceInputImpl<Person>
		implements IEntityInstanceOutput<Person> {

	private IPersonManager manager;

	public void setManager(IPersonManager manager) {
		this.manager = manager;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String exportSPF(Person pojo, IAppDataManager appDataManager) {

		SimpleEntityInstance ei = new SimpleEntityInstance("IFCPERSON",
				appDataManager.getName(pojo));

		// Set Identification
		if (pojo.getIdentification() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			StringParameter s = new StringParameter(pojo.getIdentification(),
					StringParameter.UTF_8);
			ei.addParameter(s);
		}

		// Set FamilyName
		if (pojo.getFamilyName() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			StringParameter s = new StringParameter(pojo.getFamilyName(),
					StringParameter.UTF_8);
			ei.addParameter(s);
		}

		// Set GivenName
		if (pojo.getGivenName() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			StringParameter s = new StringParameter(pojo.getGivenName(),
					StringParameter.UTF_8);
			ei.addParameter(s);
		}

		// Set MiddleNames
		if (pojo.getMiddleNames() == null || pojo.getMiddleNames().isEmpty()) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ListParameter l = new ListParameter();

			for (String s : pojo.getMiddleNames()) {
				StringParameter sp = new StringParameter(s,
						StringParameter.UTF_8);
				l.add(sp);
			}

			ei.addParameter(l);
		}

		// Set PrefixTitles
		if (pojo.getPrefixTitles() == null || pojo.getPrefixTitles().isEmpty()) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ListParameter l = new ListParameter();

			for (String s : pojo.getPrefixTitles()) {
				StringParameter sp = new StringParameter(s,
						StringParameter.UTF_8);
				l.add(sp);
			}
			ei.addParameter(l);
		}

		// Set SuffixTitles
		if (pojo.getSuffixTitles() == null || pojo.getSuffixTitles().isEmpty()) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ListParameter l = new ListParameter();

			for (String s : pojo.getSuffixTitles()) {
				StringParameter sp = new StringParameter(s,
						StringParameter.UTF_8);
				l.add(sp);
			}

			ei.addParameter(l);
		}

		// Set Roles
		if (pojo.getRoles().isEmpty()) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ListParameter l = new ListParameter();
			for (ActorRole role : pojo.getRoles()) {
				EntityInstanceNameParameter n = new EntityInstanceNameParameter(
						appDataManager.getName(role));
				l.add(n);
			}
			ei.addParameter(l);
		}

		// Set Addresses
		if (pojo.getAddresses().isEmpty()) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ListParameter l = new ListParameter();
			for (Address add : pojo.getAddresses()) {
				EntityInstanceNameParameter n = new EntityInstanceNameParameter(
						appDataManager.getName(add));
				l.add(n);
			}
			ei.addParameter(l);
		}

		return ei.toString();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected Person doCreateObject() {
		return new Person();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveReferenceParameter(EntityInstance ei, Person pojo,
			IAppDataManager appDataManager) {

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set Roles
		Parameter roles = sei.getParameter(6);
		if (roles.getNodeType() == NodeType.LIST) {
			ListParameter l = (ListParameter) roles;
			for (Parameter p : l) {
				EntityInstanceNameParameter n = (EntityInstanceNameParameter) p;
				manager.addRole(pojo, (ActorRole) appDataManager.getObject(n
						.toInstanceName()));
			}
		}

		// Set Addresses
		Parameter addresses = sei.getParameter(7);
		if (addresses.getNodeType() == NodeType.LIST) {
			ListParameter l = (ListParameter) addresses;
			for (Parameter p : l) {
				EntityInstanceNameParameter n = (EntityInstanceNameParameter) p;
				manager.addAddress(pojo,
						(Address) appDataManager.getObject(n.toInstanceName()));
			}
		}
	}

	@Override
	protected void resolveNotReferenceParameter(EntityInstance ei, Person pojo) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set Identification
		Parameter id = sei.getParameter(0);
		if (id.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) id;
			pojo.setIdentification(s.toUTF8());
		}

		// Set FamilyName
		Parameter family = sei.getParameter(1);
		if (family.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) family;
			pojo.setFamilyName(s.toUTF8());
		}

		// Set GivenName
		Parameter given = sei.getParameter(2);
		if (given.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) given;
			pojo.setGivenName(s.toUTF8());
		}

		// Set MiddleNames
		Parameter middles = sei.getParameter(3);
		if (middles.getNodeType() == NodeType.LIST) {
			ListParameter l = (ListParameter) middles;
			for (Parameter p : l) {
				StringParameter s = (StringParameter) p;
				pojo.getMiddleNames().add(s.toUTF8());
			}
		}

		// Set PrefixTitles
		Parameter prefix = sei.getParameter(4);
		if (prefix.getNodeType() == NodeType.LIST) {
			ListParameter l = (ListParameter) prefix;
			for (Parameter p : l) {
				StringParameter s = (StringParameter) p;
				pojo.getPrefixTitles().add(s.toUTF8());
			}
		}

		// Set SuffixTitles
		Parameter suffix = sei.getParameter(5);
		if (suffix.getNodeType() == NodeType.LIST) {
			ListParameter l = (ListParameter) suffix;
			for (Parameter p : l) {
				StringParameter s = (StringParameter) p;
				pojo.getSuffixTitles().add(s.toUTF8());
			}
		}
	}
}
